package com.gxp.controller.game;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.gxp.pojo.Game;
import com.gxp.pojo.GameGoods;
import com.gxp.pojo.Order;
import com.gxp.service.GameService;
import com.gxp.service.OrderService;
import com.gxp.util.common.GXPConstant;


@Controller
public class GameEquipController {
	
	Logger logger = Logger.getLogger(GameEquipController.class);

	@Autowired
	private GameService gameService;

	@Autowired
	private OrderService orderService;

	
	@RequestMapping(value="/admin/game/equipment",method=RequestMethod.GET)
	public ModelAndView getGames() {
		logger.debug("GameEquipController getGames is invoked.........");
		List<Game> games = gameService.getGames();
		ModelAndView mav = new ModelAndView();
		mav.addObject("games", games);
		mav.setViewName("admin/game/equipment");
		return mav;		
	}
	
	
	@RequestMapping(value="/admin/game/equips/get",method=RequestMethod.POST)
	@ResponseBody
	public List<GameGoods> getEquipments(Game game) {
		logger.debug("GameEquipController getEquipments is invoked.........");
		List<GameGoods> gameGoods = gameService.getEquipments(game.getGameId());
		return gameGoods;		
	}
	
	@RequestMapping(value="/admin/game/equips/save",method=RequestMethod.POST)
	@ResponseBody
	public List<GameGoods> saveGameEquipment(GameGoods goodsInput) {
		logger.debug("GameEquipController is saveGameEquipment invoked.........");
		gameService.saveEquipment(goodsInput);
		List<GameGoods> goods = gameService.getEquipments(goodsInput.getGameId());
		return goods;
	}
	
	@RequestMapping(value="/admin/game/equip/active/{gameId}/{goodsId}",method=RequestMethod.POST)
	@ResponseBody
	public List<GameGoods> activeGameEquipment(@PathVariable int gameId, @PathVariable int goodsId) {
		logger.debug("GameEquipController is activeGameEquipment invoked.........");
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_GOODS_ID, goodsId);
		param.put(GXPConstant.MAPPER_KEY_NEW_STATUS, 1);
		gameService.updateEquipStatus(param);
		List<GameGoods> goods = gameService.getEquipments(gameId);
		return goods;
	}
	
	@RequestMapping(value="/admin/game/equip/inActive/{gameId}/{goodsId}",method=RequestMethod.POST)
	@ResponseBody
	public List<GameGoods> inActiveGameEquipment(@PathVariable int gameId, @PathVariable int goodsId) {
		logger.debug("GameEquipController is inActiveGameEquipment invoked.........");
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_GOODS_ID, goodsId);
		param.put(GXPConstant.MAPPER_KEY_NEW_STATUS, 0);
		gameService.updateEquipStatus(param);
		List<GameGoods> goods = gameService.getEquipments(gameId);
		return goods;
	}
	
	@RequestMapping(value="/admin/game/equip/delete/{gameId}/{goodsId}",method=RequestMethod.POST)
	@ResponseBody
	public List<GameGoods> delGameEquipment(@PathVariable int gameId, @PathVariable int goodsId, 
			HttpServletRequest request,HttpServletResponse response) {
		logger.debug("GameEquipController is delGameEquipment invoked.........");
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put(GXPConstant.MAPPER_KEY_GOODS_ID, goodsId);
		List<Order> orders = orderService.findBuyerOrders(param1);
		List<GameGoods> goods = null;
		if (orders != null && !orders.isEmpty()) {
			response.setContentType("text/html;charset=UTF-8");
			ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
			String fileTypeError = ctx.getMessage("msg.delete.equipment.error", null, RequestContextUtils.getLocale(request));
			try {
				PrintWriter out = response.getWriter();				
				out.write("{\"ERROR\":\"" + fileTypeError + "\"}");
			    out.flush();
			    out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		} else {
			Map<String,Object> param = new HashMap<String, Object>();
			param.put(GXPConstant.MAPPER_KEY_GOODS_ID, goodsId);
			param.put(GXPConstant.MAPPER_KEY_NEW_STATUS, -1);
			gameService.updateEquipStatus(param);
			goods = gameService.getEquipments(gameId);			
		}
		
		return goods;
	}
	

	@RequestMapping(value="/admin/game/equip/update/{gameId}/{goodsId}",method=RequestMethod.POST)
	@ResponseBody
	public String updateGameEquipment(@PathVariable int gameId, @PathVariable int goodsId, GameGoods goods) {
		logger.debug("GameEquipController is updateGameEquipment invoked.........");
		goods.setGameId(gameId);
		goods.setGoodsId(goodsId);
		logger.debug("goods id: " + goodsId);
		logger.debug("gameId id: " + gameId);
		logger.debug("goods new name: " + goods.getName());
		logger.debug("goods new keywords: " + goods.getKeywords());
		gameService.updateEquip(goods);
		return "success";
	}
	
}
